<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/17
 * Time: 14:18
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Service\Admin\BoxService;
use App\Constant\ErrorConstant;

class BoxController extends BaseController
{
    protected $service;

    public function __construct(Request $request, BoxService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 盒子列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listBox()
    {
        $validator  =   Validator::make($this->request->post(), [
            'query' =>  'array'
        ], [
            'query.array'   =>  '请求参数必须为数组'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_LIST_ERROR, $validator->errors()->first());
        }

        $limit  =   $this->request->input('page_size', NULL);
        $page   =   $this->request->input('page', 1);
        $query  =   $this->request->post('query', []);
        [$flag, $result]    =   $this->service->boxList($query, (int) $limit, (int) $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_LIST_ERROR, $result);
        }
    }

    /**
     * 盒子添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addBox()
    {
        $validator  =   Validator::make($this->request->post(), [
            'box_name'  =>  'required|string',
            'box_code'  =>  'required|string',
            'owner_id'  =>  'required|integer|exists:mini_box_owner,id',
            'shop_id'   =>  'required|integer|exists:mini_shop,id',
            'area_id'   =>  'required|integer|exists:mini_box_area,id',
            'parent_id' =>  'integer|exists:mini_box,id'
        ], [
            'box_name.required' =>  '盒子名称不能为空',
            'box_name.string'   =>  '盒子名称必须为字符',

            'box_code.required' =>  '盒子编号不能为空',
            'box_code.string'   =>  '盒子编号必须为字符',

            'owner_id.required' =>  '盒子用户ID不能为空',
            'owner_id.integer'  =>  '盒子用户ID必须为整型',
            'owner_id.exists'   =>  '盒子用户不存在',

            'shop_id.required' =>  '店铺ID不能为空',
            'shop_id.integer'  =>  '店铺ID必须为整型',
            'shop_id.exists'   =>  '店铺不存在',

            'area_id.required' =>  '盒子地区ID不能为空',
            'area_id.integer'  =>  '盒子地区ID必须为整型',
            'area_id.exists'   =>  '盒子地区不存在',

            'parent_id.integer'=>  '上级盒子ID必须为整型',
            'parent_id.exists' =>  '上级盒子不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->boxAdd($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_ADD_ERROR, $result);
        }
    }

    /**
     * 删除盒子
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteBox()
    {
        $validator  =   Validator::make($this->request->post(), [
            'box_id' =>  'required|integer|exists:mini_box,id'
        ], [
            'box_id.required'   =>  '盒子ID不能为空',
            'box_id.integer'    =>  '盒子ID必须为整型',
            'box_id.exists'     =>  '盒子不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_DELETE_ERROR, $validator->errors()->first());
        }

        $box_id  =   (int) $this->request->post('box_id');
        [$flag, $result]    =   $this->service->boxDelete($box_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_DELETE_ERROR, $result);
        }
    }

    /**
     * 盒子详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoBox()
    {
        $validator  =   Validator::make($this->request->post(), [
            'box_id' =>  'required|integer|exists:mini_box,id'
        ], [
            'box_id.required'   =>  '盒子ID不能为空',
            'box_id.integer'    =>  '盒子ID必须为整型',
            'box_id.exists'     =>  '盒子不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_DELETE_ERROR, $validator->errors()->first());
        }

        $box_id  =   (int) $this->request->post('box_id');
        [$flag, $result]    =   $this->service->boxInfo($box_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_DELETE_ERROR, $result);
        }
    }

    /**
     * 盒子编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function editBox()
    {
        $validator  =   Validator::make($this->request->post(), [
            'box_id'    =>  'required|integer|exists:mini_box,id',
            'box_name'  =>  'required|string',
            'box_code'  =>  'required|string',
            'owner_id'  =>  'required|integer|exists:mini_box_owner,id',
            'shop_id'   =>  'required|integer|exists:mini_shop,id',
            'area_id'   =>  'required|integer|exists:mini_box_area,id',
            'parent_id' =>  'integer|exists:mini_box,id'
        ], [
            'box_id.required'   =>  '盒子ID不能为空',
            'box_id.integer'    =>  '盒子ID必须为整型',
            'box_id.exists'     =>  '盒子不存在',

            'box_name.required' =>  '盒子名称不能为空',
            'box_name.string'   =>  '盒子名称必须为字符',

            'box_code.required' =>  '盒子编号不能为空',
            'box_code.string'   =>  '盒子编号必须为字符',

            'owner_id.required' =>  '盒子用户ID不能为空',
            'owner_id.integer'  =>  '盒子用户ID必须为整型',
            'owner_id.exists'   =>  '盒子用户不存在',

            'shop_id.required' =>  '店铺ID不能为空',
            'shop_id.integer'  =>  '店铺ID必须为整型',
            'shop_id.exists'   =>  '店铺不存在',

            'area_id.required' =>  '盒子地区ID不能为空',
            'area_id.integer'  =>  '盒子地区ID必须为整型',
            'area_id.exists'   =>  '盒子地区不存在',

            'parent_id.integer'=>  '上级盒子ID必须为整型',
            'parent_id.exists' =>  '上级盒子不存在',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_EDIT_ERROR, $validator->errors()->first());
        }

        $box_id  =   (int) $this->request->post('box_id');

        [$flag, $result]    =   $this->service->boxEdit($this->request->post(), $box_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_BOX_EDIT_ERROR, $result);
        }
    }
}
